import { defineStore } from 'pinia'
import { ref } from 'vue'

export interface Elderly {
  id: string
  name: string
  age: number
  sex: '男' | '女'
  phone: string
  address: string
  weight: string
  height: string
  bloodType: string
  emergencyContact: {
    name: string
    phone: string
    relationship: string
  }
  medicalHistory: string[]
  allergies: string[]
  createdAt: string
  updatedAt: string
}

export interface HealthRecord {
  id: string
  elderlyId: string
  date: string
  systolic: number
  diastolic: number
  bloodSugar: number
  weight: number
  height: number
  temperature: number
  heartRate: number
  notes: string
}

export interface MedicationRecord {
  id: string
  elderlyId: string
  date: string
  medicationName: string
  dosage: string
  frequency: string
  duration: string
  status: '正在服用' | '已完成' | '已暂停'
  notes: string
}

export const useElderlyStore = defineStore('elderly', () => {
  const elderlyList = ref<Elderly[]>([])
  const healthRecords = ref<HealthRecord[]>([])
  const medicationRecords = ref<MedicationRecord[]>([])
  // 在elderly.ts中的setElderlyList方法
const setElderlyList = (list: Elderly[]) => {
  elderlyList.value = list
}

  const addElderly = (elderly: Omit<Elderly, 'id' | 'createdAt' | 'updatedAt'>) => {
    const newElderly: Elderly = {
      ...elderly,
      id: Date.now().toString(),
      createdAt: new Date().toISOString(),
      updatedAt: new Date().toISOString()
    }
    elderlyList.value.push(newElderly)
  }

  const updateElderly = (id: string, updates: Partial<Elderly>) => {
    const index = elderlyList.value.findIndex(elderly => elderly.id === id)
    if (index !== -1) {
      elderlyList.value[index] = {
        ...elderlyList.value[index],
        ...updates,
        updatedAt: new Date().toISOString()
      }
    }
  }

  const deleteElderly = (id: string) => {
    const index = elderlyList.value.findIndex(elderly => elderly.id === id)
    if (index !== -1) {
      elderlyList.value.splice(index, 1)
    }
  }

  const addHealthRecord = (elderlyId: string, p0: { date: string; systolic: null; diastolic: null; bloodSugar: null; weight: null; temperature: null; heartRate: null; notes: string }, record: Omit<HealthRecord, 'id'>) => {
    const newRecord: HealthRecord = {
      ...record,
      id: Date.now().toString()
    }
    healthRecords.value.push(newRecord)
  }

  const addMedicationRecord = (record: Omit<MedicationRecord, 'id'>) => {
    const newRecord: MedicationRecord = {
      ...record,
      id: Date.now().toString()
    }
    medicationRecords.value.push(newRecord)
  }

  const getElderlyById = (id: string) => {
    return elderlyList.value.find(elderly => elderly.id === id)
  }

  const getHealthRecordsByElderlyId = (elderlyId: string) => {
    return healthRecords.value.filter(record => record.elderlyId === elderlyId)
  }

  const getMedicationRecordsByElderlyId = (elderlyId: string) => {
    return medicationRecords.value.filter(record => record.elderlyId === elderlyId)
  }

  return {
    elderlyList,
    healthRecords,
    medicationRecords,
    addElderly,
    updateElderly,
    deleteElderly,
    addHealthRecord,
    addMedicationRecord,
    getElderlyById,
    getHealthRecordsByElderlyId,
    getMedicationRecordsByElderlyId,
    setElderlyList
  }
}) 